package com.ruoyi.a2.service.impl;

import com.ruoyi.a2.domain.A2DynamicsInfo;
import com.ruoyi.a2.domain.A2Pdcc;
import com.ruoyi.a2.service.IA2DynamicsInfoService;
import com.ruoyi.a2.service.IA2StaticInfoService;
import com.ruoyi.a2.service.ITool2aEOfAbnormalValueService;
import com.ruoyi.common.enums.CleanStatusEnum;
import com.ruoyi.common.enums.CommonOKNOEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Service
public class Tool2aEOfAbnormalValueImpl implements ITool2aEOfAbnormalValueService
{

    @Autowired
    private IA2DynamicsInfoService a2DynamicsInfoService;

    /**
     *  异常值剔除
     *  实现思路如下：
     * （1）然后循环所有点位，求出当前点位与前一个点位的经度差值，当前点位与前一个点位的纬度差值
     * （2）如果经度或纬度差值大于A2Pdcc.threshold（阈值），则判定为异常值
     *
     * @author :yanlixiong
     * @date :2025-02-17 09:28:54
     */
    @Override
    public List<A2DynamicsInfo> handle(List<A2DynamicsInfo> infoList, Integer threshold) {
        int size = infoList.size();
        for (int i = 0; i <size ; i++) {
            A2DynamicsInfo info = infoList.get(i);
            int indexBefore = i>0?i-1:i;
            A2DynamicsInfo infoBefore = infoList.get(indexBefore);
            if(info.getLon()!=null && info.getLat()!=null && infoBefore.getLon()!=null && infoBefore.getLat()!=null ) {
                double lonDifference = Math.abs(infoBefore.getLon() - info.getLon());
                double latDifference = Math.abs(infoBefore.getLat() - info.getLat());
                if (lonDifference > threshold || latDifference > threshold) {
                    info.setCleanStatus(CleanStatusEnum.DATA_1.getCode());
                }
            }
            info.setHandleCleanStatus1(CommonOKNOEnum.OK.getCode());
            a2DynamicsInfoService.updateA2DynamicsInfo(info);
        }
        return infoList;
    }
}
